﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BangleCMS.DBUtility;

namespace BangleCMS.DAL
{
    public class DBUtility
    {
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="Field"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <param name="desc"></param>
        /// <param name="startIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public DataSet GetListByPage(string TableName, string Field, string strWhere, string orderby, int desc, int startIndex, int pageSize)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM " + TableName);
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ORDER BY " + orderby + (desc == 1 ? " DESC " : ""));
            strSql.AppendFormat(" LIMIT {0},{1}", startIndex, pageSize);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int GetRecordCount(string TableName, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + TableName);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperMySQL.Query(strSql.ToString()).Tables[0].Rows[0][0]);
        }
    }
}
